﻿using System.ComponentModel.Composition;
using ExcelAutomator.Validation;

namespace ExcelAutomator
{
	public abstract class Executor<TExecutorConfiguration> : IExecutor
	{
		public abstract string Name { get; }

		public abstract string Type { get; }

		public abstract string ActionId { get; }
		

		#region ParameterPatternParser

		[Import(typeof(IParameterPatternParser))]
		private IParameterPatternParser m_ParameterPatternParser;

		#endregion


		private TExecutorConfiguration m_Configuration;
		public TExecutorConfiguration Configuration
		{
			get { return m_Configuration; }
		}

		public ExecutionResult Execute(ExecutionContext context)
		{
			return OnExecute(context);
		}

		public abstract ExecutionResult OnExecute(ExecutionContext context);

		public void Configure(string xmlConfiguration)
		{
			OnConfigure(xmlConfiguration, out m_Configuration);
		}

		public abstract void OnConfigure(string xmlConfiguration, out TExecutorConfiguration deserialiedConfiguration); 

		public void Validate(ValidationEngine validationEngine, ExecutionContext context)
		{
			m_Configuration = m_ParameterPatternParser.Parse(Configuration, context);
			OnValidate(validationEngine, context);
		}

		public abstract void OnValidate(ValidationEngine validationEngine, ExecutionContext context);
	}
}
